<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="雪场名称" prop="snowField">
                <el-input v-model="queryParams.snowField" placeholder="请输入雪场名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:info:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:info:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:info:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:info:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="雪场名称" align="center" prop="snowField" show-overflow-tooltip />
            <el-table-column label="省市区" align="center" prop="snowAddressPath" show-overflow-tooltip />
            <el-table-column label="详细地址" align="center" prop="snowAddressDetail" show-overflow-tooltip />
            <el-table-column label="经纬度" align="center" prop="snowCenter" show-overflow-tooltip />
            <el-table-column label="雪场简介" align="center" prop="snowDesc" show-overflow-tooltip />
            <el-table-column label="雪场关闭时间" align="center" prop="snowEndTime" width="180" />
            <el-table-column label="雪场开始时间" align="center" prop="snowStartTime" width="180" />
            <el-table-column label="雪场状态" align="center" prop="snowStatus">
                <template slot-scope="scope">
                    <el-tag type="success" size="small" v-if="scope.row.snowStatus == 'open'">启用</el-tag>
                    <el-tag type="info" size="small" v-if="scope.row.snowStatus == 'close'">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
                        v-hasPermi="['system:info:query']">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:info:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:info:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改雪场信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="雪场名称" prop="snowField">
                            <el-input v-model="form.snowField" placeholder="请输入雪场名称" />
                        </el-form-item>
                        <el-form-item label="雪场开始时间" prop="snowStartTime">
                            <el-time-picker v-model="form.snowStartTime" clearable value-format="HH:mm:ss"
                                placeholder="请选择雪场开始时间">
                            </el-time-picker>
                        </el-form-item>
                        <!-- <el-form-item label="省市区" prop="snowAddressPath">
                            <el-cascader v-model="form.snowAddressPath" :options="options" :props="props"
                                @change="handleChangeCounty"></el-cascader>
                        </el-form-item> -->
                        <el-form-item label="滑雪定位" prop="snowCenter">
                            <el-button type="primary" @click="handleSnowCenter">
                                {{ form.snowCenter ? '已定位' : '点击获雪场铺定位' }}
                            </el-button>
                        </el-form-item>

                        <el-form-item label="雪场状态" prop="status">
                            <el-switch v-model="form.status"></el-switch>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="雪场简介" prop="snowDesc">
                            <el-input v-model="form.snowDesc" placeholder="请输入雪场简介" />
                        </el-form-item>
                        <el-form-item label="雪场关闭时间" prop="snowEndTime">
                            <el-time-picker v-model="form.snowEndTime" clearable value-format="HH:mm:ss"
                                placeholder="请选择雪场关闭时间">
                            </el-time-picker>
                        </el-form-item>
                        <el-form-item label="详细地址" prop="snowAddressDetail">
                            <el-input v-model="form.snowAddressDetail" placeholder="请输入详细地址" />
                        </el-form-item>

                    </el-col>
                </el-row>
                <el-form-item label="选择套餐" prop="setMealIds">
                    <el-select style="width: 100%" v-model="form.setMealIds" multiple placeholder="请选择套餐">
                        <el-option v-for="item in options" :key="item.id" :label="item.setMealName" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="title != '雪场信息详情'">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>

        <liliMap ref="liliMap" @getAddress="getAddress"></liliMap>
    </div>
</template>
  
<script>
import liliMap from "@/components/Map/index";
// import { AllCounty } from '@/api/public'
import { listInfo, listMeal, addInfo, updateInfo, getInfo, delInfo } from '@/api/business/info'
export default {
    name: "Info",
    components: {
        liliMap
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 雪场信息表格数据
            infoList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                snowField: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                snowField: [{ required: true, message: '请输入雪场名称', trigger: 'blur' }],
                snowDesc: [{ required: true, message: '请输入雪场简介', trigger: 'blur' }],
                snowStartTime: [{ required: true, message: '请选择雪场开始时间', trigger: 'change' }],
                snowEndTime: [{ required: true, message: '请选择雪场关闭时间', trigger: 'change' }],
                snowCenter: [{ required: true, message: '请选择雪场定位', trigger: 'change' }],
                snowAddressDetail: [{ required: true, message: '请输入雪场详细地址', trigger: 'blur' }]
            },
            // 省市区数据
            // options: [],
            // props: {
            //     lazy: true,
            //     checkStrictly: true,
            //     lazyLoad: (node, resolve) => {
            //         const { level } = node // node 节点数据 
            //         const nodes = []  // 动态节点
            //         let type = level == 0 ? "0" : node.value  // 0 代表第一次请求
            //         this.provinceFn(type).then(res => {
            //             res.data.map((item) => {
            //                 let area = {
            //                     value: item.id,
            //                     label: item.name,
            //                     leaf: node.level >= 3,
            //                 }
            //                 nodes.push(area)
            //             })
            //             resolve(nodes) // 回调
            //         }).catch((error) => {
            //             console.log('error----------: ', error)
            //         })
            //     }
            // }
            // 套餐信息列表
            options: [],
        };
    },
    created() {
        this.getList()
    },
    methods: {
        // 获取省市区街道
        // provinceFn(id) {
        //     return AllCounty(id)
        // },

        /**省市区change */
        // handleChangeCounty(e) {
        //     console.log('e--------: ', e)
        // },

        /**点击获雪场铺定位 */
        handleSnowCenter() {
            this.$refs.liliMap.showMap = true
            this.$refs.liliMap.init()
        },
        /**点击定位获取店铺地址 */
        getAddress(item) {
            console.log('item----------: ', item)
            this.form.snowCenter = item.position.lng + "," + item.position.lat;
            this.form.snowAddressDetail = item.address
            this.form.snowAddressPath = item.addr
            this.form.snowAddressIdPath = item.addrId
            this.form.adcode = item.regeocode.addressComponent.adcode
            this.form.latitude = item.position.lat
            this.form.longitude = item.position.lng
        },

        /** 查询雪场信息列表 */
        getList() {
            this.loading = false;
            listInfo(this.queryParams).then(response => {
                // console.log('response---------: ', response)
                this.infoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /**查询套餐信息列表 */
        getMealList() {
            listMeal().then(response => {
                // console.log('response----------: ', response)
                this.options = response.rows
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                // delFlag: null,
                status: true,
                // snowStatus: 'open',
                snowField: null,
                snowStartTime: null,
                snowEndTime: null,
                snowAddressDetail: null,
                snowAddressIdPath: null,
                snowAddressPath: null,
                snowCenter: null,
                snowDesc: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /**详情按钮操作 */
        handleDetail(row) {
            this.reset();
            const id = row.id || this.ids
            getInfo(id).then(response => {
                this.getMealList()
                let ids = response.data.skiSetMealList.map(v => v.id);
                this.form = response.data.skiSnowField;
                this.$set(this.form, 'setMealIds', ids)
                if (this.form.snowStatus == 'open') {
                    this.$set(this.form, 'status', true)
                } else {
                    this.$set(this.form, 'status', false)
                }
                // console.log('this.form-----------: ', this.form)
                this.open = true;
                this.title = "雪场信息详情";
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加雪场信息";
            this.getMealList()
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getInfo(id).then(response => {
                console.log('response----------: ', response)
                this.getMealList()
                let ids = response.data.skiSetMealList.map(v => v.id)
                this.form = response.data.skiSnowField;
                this.$set(this.form, 'setMealIds', ids)
                this.$set(this.form, 'snowFieldId', response.data.skiSnowField.id)
                if (this.form.snowStatus == 'open') {
                    this.$set(this.form, 'status', true)
                } else {
                    this.$set(this.form, 'status', false)
                }
                this.open = true;
                this.title = "修改雪场信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.form.snowStatus = this.form.status == true ? 'open' : 'close'
            console.log('this.form----------: ', this.form)
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        // console.log('修改成功')
                        updateInfo(this.form.id, this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        // console.log('新增成功')
                        addInfo(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除雪场信息编号为"' + ids + '"的数据项？').then(function () {
                return delInfo(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/system/snowField/export', {
                ...this.queryParams
            }, `info_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  